package io.wdb.modules.generator.generator.service.impl.book;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.wdb.common.utils.Query;
import io.wdb.modules.generator.generator.dao.BookTagMappingDao;
import io.wdb.modules.generator.generator.entity.BookTagMappingEntity;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.book.BookLabelService;

@Service
public class BookLabelServiceImpl implements BookLabelService{

	@Autowired
	private BookTagMappingDao bookTagMappingDao;
	
	@Override
	public List<BookTagMappingEntity> queryList(Query query) {
		return bookTagMappingDao.queryList(query);
	}

	@Override
	public int queryTotal(Query query) {
		return bookTagMappingDao.queryTotal();
	}

	@Override
	public WebResponse updateLabel(BookTagMappingEntity bookTagMappingEntity) {
		WebResponse webResponse = new WebResponse();
		//查找数据库中的
		BookTagMappingEntity bookTagMappingEntityDb = bookTagMappingDao.queryObject(bookTagMappingEntity.getId());
		bookTagMappingEntityDb.setDescription(bookTagMappingEntity.getDescription());
		bookTagMappingEntityDb.setTagName(bookTagMappingEntity.getTagName());
		bookTagMappingDao.update(bookTagMappingEntityDb);
		return null;
	}

	@Override
	public WebResponse insertLabel(BookTagMappingEntity bookTagMappingEntity) {
		WebResponse webResponse = new WebResponse();
		Integer maxTagCode = bookTagMappingDao.selectMaxTagCode();
		if(bookTagMappingEntity.getTagCode()==null) {
			String maxTagCodeStr = maxTagCode+"";
			int length = maxTagCodeStr.length();
			for(int i=0;i<4-length;i++) {
				maxTagCodeStr="0"+maxTagCodeStr;
			}
			bookTagMappingEntity.setTagCode(maxTagCodeStr);
		}
		bookTagMappingDao.save(bookTagMappingEntity);
		return null;
	}

	@Override
	public WebResponse deleteLable(Long id) {
		WebResponse webResponse = new WebResponse();
		bookTagMappingDao.delete(id);
		return null;
	}

}
